package com.quectel.aiot.controller;

import com.quectel.aiot.entity.Role;
import com.quectel.aiot.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理", description = "角色管理相关接口")
public class RoleController {
    @Autowired
    private RoleService roleService;

    // 分页条件查询角色列表
    @GetMapping("/list")
    @ApiOperation("分页条件查询角色列表")
    public Map<String, Object> list(
            @ApiParam(name = "name", value = "角色名称") @RequestParam(required = false) String name,
            @ApiParam(name = "appId", value = "应用ID") @RequestParam(required = false) String appId,
            @ApiParam(name = "page", value = "当前页码", defaultValue = "1") @RequestParam(defaultValue = "1") int page,
            @ApiParam(name = "size", value = "每页数量", defaultValue = "10") @RequestParam(defaultValue = "10") int size) {
        List<Role> list = roleService.getRoleList(name, appId, page, size);
        int total = roleService.getRoleCount(name, appId);
        Map<String, Object> result = new HashMap<>();
        result.put("data", list);
        result.put("total", total);
        return result;
    }

    // 查询单个角色详情
    @GetMapping("/{id}")
    @ApiOperation("查询单个角色详情")
    public Map<String, Object> get(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        Role role = roleService.getRoleById(id);
        if (role == null) {
            result.put("success", false);
            result.put("message", "角色不存在");
            result.put("data", null);
        } else {
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", role);
        }
        return result;
    }

    // 新增角色
    @PostMapping
    @ApiOperation("新增角色")
    public Map<String, Object> add(@ApiParam(name = "role", value = "角色信息", required = true) @RequestBody Role role) {
        roleService.addRole(role);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "角色新增成功");
        result.put("id", role.getId());
        return result;
    }

    // 修改角色
    @PutMapping
    @ApiOperation("修改角色")
    public Map<String, Object> update(@ApiParam(name = "role", value = "角色信息", required = true) @RequestBody Role role) {
        Map<String, Object> result = new HashMap<>();
        Role old = roleService.getRoleById(role.getId());
        if (old == null) {
            result.put("success", false);
            result.put("message", "角色不存在");
            return result;
        }
        int updated = roleService.updateRole(role);
        if (updated > 0) {
            result.put("success", true);
            result.put("message", "角色更新成功");
        } else {
            result.put("success", false);
            result.put("message", "未做任何修改");
        }
        return result;
    }

    // 删除角色
    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    public Map<String, Object> delete(
            @ApiParam(name = "id", value = "角色ID", required = true) @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        Role role = roleService.getRoleById(id);
        if (role == null) {
            result.put("success", false);
            result.put("message", "角色不存在，无法删除");
        } else {
            roleService.deleteRole(id);
            result.put("success", true);
            result.put("message", "角色删除成功");
        }
        return result;
    }
} 